/*
 * This file is part of ddesktop: remote desktop system for iPad 
 *
 * Copyright (C) 2010 Kazuhiro Satoh
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, see <http://www.gnu.org/licenses/>.
 */


#import "SettingViewController.h"
#import "SettingControl.h"


@implementation SettingViewController


-(int) tableWidth
{
	CGRect rect = [UIScreen mainScreen].bounds;
	return MIN(rect.size.width, rect.size.height) / 2;
}


-(CGSize) contentSizeForViewInPopover
{
	const int MARGIN = 16;
	
	return CGSizeMake(self.tableWidth + MARGIN * 2,
					  self.tableView.sectionFooterHeight +
					  [self.tableView rectForSection:0].size.height +
					  [self.tableView rectForSection:0].origin.y +
					  MARGIN);
}


-(id) initWithSetting:(Setting*)setting
{
	if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
		_setting = setting;
	}
	return self;
}


- (void)dealloc{
	[super dealloc];
}

/*
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
	return @"DDesktopClient ver.0.2\n(c)2010 Kazuhiro Satoh\nhttp://code.google.com/p/ddesktop/";
}
*/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [[_setting directoryForKeyPath:@"Global"] itemCount];
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {	
	return @"General Settings";
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell;
	
	if(indexPath.section == 0){ // Setting
		cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithSlider"];
		
		if (cell == nil) {
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellWithSlider"] autorelease];
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
		}
		
		SettingItem* item = [[_setting directoryForKeyPath:@"Global"] itemAtIndex:indexPath.row];
		UIControl* control = [SettingControl controlForSettingItem:item withFrame:CGRectMake(0, 0, self.tableWidth / 2, 0)];
		cell.accessoryView = control;
		cell.textLabel.text = (NSString*)[item metaData:@"title"];
	}
	return cell;
}

/*
 To conform to Human Interface Guildelines, since selecting a row would have no effect (such as navigation), make sure that rows cannot be selected.
 */
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	return nil;
}


@end
